package com.binguo.wxapi.controller;

import com.binguo.common.enums.CodeType;
import com.binguo.model.wxapi.vo.CustomerVO;
import com.binguo.service.SMSservice;
import com.binguo.wxapi.form.*;
import com.binguo.wxapi.result.Result;
import com.binguo.wxapi.result.ResultGenerator;
import com.binguo.wxapi.service.WXLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;


/**
 */
@Api(tags = "小程序: 授权/上传信息")
@RestController
@RequestMapping
public class WXLoginController {

    private static Logger logger = LoggerFactory.getLogger(WXLoginController.class);


    @Autowired
    WXLoginService WXLoginService;

    @Autowired
    SMSservice smSservice;

    @PostMapping(value = "/api/wxapp/authorizeTemp", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "临时授权", notes = "通过微信code")
    public Result authorizeTemp(@RequestBody @Valid CodeForm form) {
        CustomerVO vo = WXLoginService.authorizeTemp(form.getCode());
        return ResultGenerator.genSuccessResult(vo);
    }

    @PostMapping(value = "/api/wxapp/ordering/authorizeOrdering", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "临时授权", notes = "通过微信code")
    public Result authorizeOrdering(@RequestBody @Valid CodeForm form) {
        CustomerVO vo = WXLoginService.authorizeOrdering(form.getCode());
        return ResultGenerator.genSuccessResult(vo);
    }


    @PostMapping(value = "/api/wxapp/authorize", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "授权", notes = "通过微信code")
    public Result authorize(@RequestBody @Valid CodeForm form) {
        CustomerVO vo = WXLoginService.authorizeTemp(form.getCode());
        return ResultGenerator.genSuccessResult(vo);
    }



    @PostMapping(value = "/api/wxapp/phoneNew", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "上传手机号", notes = "通过微信code")
    public Result phoneNew(@RequestBody @Valid WxPhoneForm form) {

        CustomerVO vo = WXLoginService.wxTempUserPhone(form.getOpenId(), form.getEncryptedData(), form.getIv());
        return ResultGenerator.genSuccessResult(vo);
    }

    @PostMapping(value = "/api/wxapp/ordering/phoneNew", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "上传手机号", notes = "通过微信code")
    public Result orderingPhoneNew(@RequestBody @Valid WxPhoneForm form) {
        CustomerVO vo = WXLoginService.wxOrderingUserPhone(form.getOpenId(), form.getEncryptedData(), form.getIv());
        return ResultGenerator.genSuccessResult(vo);
    }


    @PostMapping(value = "/api/wxapp/userInfoNew", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "上传userInfo", notes = "通过UserInfoForm")
    public Result userinfo(@RequestBody @Valid UserInfoDetailForm userInfoDetailForm) {
        WXLoginService.userInfoDetail(userInfoDetailForm);
        return ResultGenerator.genSuccessResult();
    }


    @PostMapping(value = "/api/wxapp/ordering/userInfoNew", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "上传userInfo", notes = "通过UserInfoForm")
    public Result orderingUserinfo(@RequestBody @Valid UserInfoDetailForm userInfoDetailForm) {
        WXLoginService.userInfoDetailOrdering(userInfoDetailForm);
        return ResultGenerator.genSuccessResult();
    }



    @PostMapping(value = "/api/wxapp/userinfo", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "上传userInfo", notes = "通过UserInfoForm")
    public Result userinfo(@RequestBody @Valid UserInfoForm form) {

        CustomerVO vo = WXLoginService.userInfo(form.getOpenId(), form.getEncryptedData(), form.getIv());
        return ResultGenerator.genSuccessResult(vo);
    }


    @PostMapping(value = "/api/wxapp/phone", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "上传手机号", notes = "通过微信code")
    public Result wxPhone(@RequestBody @Valid WxPhoneForm form) {

        CustomerVO vo = WXLoginService.wxPhone(form.getOpenId(), form.getEncryptedData(), form.getIv());
        return ResultGenerator.genSuccessResult(vo);
    }

    @PostMapping(value = "/api/wxapp/phoneByCode", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取手机号", notes = "通过微信code")
    public Result phoneByCode(@RequestBody @Valid WxPhoneCodeForm form) {

        String phone = WXLoginService.getPhoneByWXCode(form.getCode(), form.getEncryptedData(), form.getIv());
        return ResultGenerator.genSuccessResult(phone);
    }


    @GetMapping(value = "/api/wxapp/phone/code", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取手机号验证码", notes = "通过手机号")
    public Result getPhoneCode(@ApiParam("手机号") @RequestParam @NotNull String phone) {
        smSservice.sendInitCode(phone, CodeType.WXAPPCODE);
        return ResultGenerator.genSuccessResult();
    }


    @PostMapping(value = "/api/wxapp/phone/checkandsave", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "手动上传手机号", notes = "通过短信验证码,手机号")
    public Result checkandsave(@RequestBody @Valid UpdatePhoneForm form) {
        if ("10000000000".equals(form.getPhone()) && "8888".equals(form.getCode())) {
            logger.info("测试账号登录：{}", form.getPhone());
        }else {
            smSservice.checkInitCode(form.getPhone(), form.getCode(), CodeType.WXAPPCODE);
        }
        CustomerVO vo = WXLoginService.updatePhone(form.getCustomerId(), form.getPhone());
        return ResultGenerator.genSuccessResult(vo);
    }


    @ApiOperation("验证短信验证码")
    @PostMapping(value = "/api/wxapp/verifySmsCode")
    public Result verifyCode(String openId, String phone, String verify) {
        if ("10000000000".equals(phone) && "8888".equals(verify)) {
            logger.info("测试账号登录：{}", phone);
        }else {
            smSservice.checkInitCode(phone, verify, CodeType.WXAPPCODE);
        }
        CustomerVO vo = WXLoginService.saveWxTempUserPhone(openId, phone);
        return ResultGenerator.genSuccessResult(vo);
    }
}
